﻿@page "/Login"
@using System.Security.Claims
@attribute [AllowAnonymous]
@layout Layout.EmptyLayout
<PageTitle>@AppData.AppConfig.AppName</PageTitle>
    <EditForm Model="Model" class="pa-0" OnValidSubmit="SubmitAsync" style="padding:0px;">
        <DataAnnotationsValidator></DataAnnotationsValidator>
        <MudCard Style="width:300px;padding:0px;" Elevation="1">
            <MudCardMedia Image="images/login.jpg" Height="100" />
            <MudCardContent Class="ma-2">
                <MudText Typo="Typo.h5" Class="pb-0"> @AppData.AppConfig.AppName </MudText>
                <MudTextField @bind-Value="Model.UserName" Label="账户" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Person" />
                <ValidationMessage For="()=>Model.UserName"></ValidationMessage>
                <MudTextField @bind-Value="Model.Password" Label="密码" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Key" InputType="InputType.Password" />
                <ValidationMessage For="()=>Model.Password"></ValidationMessage>
            </MudCardContent>
            <MudCardActions Class="ma-2">
                <MudButton Disabled="@IsLoading" ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Primary" Class="btn" FullWidth="true" Style="border-radius: 25px;">
                    @if (IsLoading)
                {
                    <MudProgressCircular Class="ms-n1" Size="Size.Small" Indeterminate="true" />
                    <MudText Class="ms-2" Style="color:aliceblue">正在登录</MudText>
                }
                else
                {
                    <MudText>登录</MudText>
                }
            </MudButton>
        </MudCardActions>
    </MudCard>
</EditForm>

@code {
    LoginViewModel Model { get; set; } = new();
    bool IsLoading { get; set; }

    [Inject] IAuthenticationService? AuthenticationService { get; set; }
    [Inject] NavigationManager? NavigationManager { get; set; }
    [Inject] ISnackbar? Snackbar { get; set; }

    async Task SubmitAsync(EditContext context)
    {
        IsLoading = true;
        try
        {
            var login = await AuthenticationService?.SignInAsync(Model)!;
            if (login != null && login.IsSuccess)
            {
                NavigationManager?.NavigateTo("/", true);
            }
            else
            {
                Snackbar?.Add("错误的账号或密码", Severity.Warning);
            }
        }
        catch (InvalidOperationException ex)
        {
            Snackbar?.Add(ex.Message, Severity.Error);
        }

        IsLoading = false;
    }

    private string? authMessage;
    private string? surname;
    private IEnumerable<Claim> claims = Enumerable.Empty<Claim>();
    @inject AuthenticationStateProvider AuthenticationStateProvider

    private async Task GetClaimsPrincipalData()
    {
        var authState = await AuthenticationStateProvider
            .GetAuthenticationStateAsync();
        var user = authState.User;

        if (user.Identity is not null && user.Identity.IsAuthenticated)
        {
            authMessage = $"{user.Identity.Name} is authenticated.";
            claims = user.Claims;
            surname = user.FindFirst(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
        }
        else
        {
            authMessage = "The user is NOT authenticated.";
        }
    }
}